package com.marshal.aop;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

//切面类
@Component
@Aspect
public class MyAdvice {
    private Logger logger = LoggerFactory.getLogger(MyAdvice.class);
    //切点
    @Pointcut(value="execution(* com.marshal.aop.*.*(..))")
    public void myPointCut() {

    }

    @Around("myPointCut()")
    public Object MyLogger(ProceedingJoinPoint pjp) throws Throwable {
        //获取要增强的类名
        String className = pjp.getTarget().getClass().toString();
        //获取要增强的方法名
        String methodName = pjp.getSignature().getName();
        //获取要增强方法的参数
        Object[] args = pjp.getArgs();

        ObjectMapper mapper=new ObjectMapper();

        logger.info("调用前传递的参数："+className+methodName+mapper.writeValueAsString(args));
        Object obj = pjp.proceed();
        logger.info("调用后返回值是："+className+methodName+mapper.writeValueAsString(obj));
        return obj;
    }
}
